<?php
    //config themes     
    require_once(CAKE .'Model/ConnectionManager.php');
    $db = ConnectionManager::getDataSource('default');    
    //get config sub site
    $sql = "SELECT *";
    $sql .= " FROM sub_sites";
    $sub_site = $db->query($sql);   
     
    if(empty($sub_site)){echo "Lỗi: Bạn chưa cấu hình thông tin site! Vui lòng kiểm tra lại !";die;}          
    //get config themes
    $sql = "SELECT *";
    $sql .= " FROM config_themes";
    $config_themes = $db->query($sql);
    
    if(empty($config_themes)){echo "Lỗi: Themes chưa được cài đặt. Vui lòng kiểm tra lại !";die;}    
    foreach($sub_site as $k=>$v){
        foreach($config_themes as $m=>$n){          
            if($n['config_themes']['id']==$v['sub_sites']['theme_id']){                 
                $sub_site[$k]['sub_sites']['themes'] = $n['config_themes'];               
                break;                    
            }          
        }  
    }     
    $_CONFIGS_THEMES = Set::combine($config_themes,'{n}.config_themes.id','{n}.config_themes.themes_name');
    
    $_THEMES = array_values($_CONFIGS_THEMES);     
    $_SERVER_NAME = $_SERVER['SERVER_NAME'];    
    $config['App']['themes']['installed'] = $_THEMES; 
    $config['App']['themes']['enabled'] = $_THEMES;     
    Configure::write('App.themes.sub_sites',$sub_site);            
    Configure::write('App.themes.listTheme',$_CONFIGS_THEMES);      
    Configure::write('App.themes.listSubSite',Set::combine($sub_site,'{n}.sub_sites.id','{n}.sub_sites.subsite_name'));
    $front = "";         
    foreach($sub_site as $k=>$v){                
        if(strcmp($_SERVER_NAME,$v['sub_sites']['sub_site'])==0){
            Configure::write('App.themes.subsite_current',$v['sub_sites']);             
            Configure::write('App.themes.site_current',$v); 
            $sql = "SELECT *";
            $sql .= " FROM config_themes";
            $sql .= " where config_themes.id = ".$v['sub_sites']['theme_id']; 
            $_THEMES_NAME = $db->query($sql);
            if(!empty($_THEMES_NAME)){                                  
                $_THEMES_NAME = Set::combine($_THEMES_NAME,'{n}.config_themes.id','{n}.config_themes.themes_name');                                
                $front = array_shift($_THEMES_NAME);                             
            }                                  
            break;
        }
    }    
    if(empty($front)){echo "Lỗi: Themes này không tồn tại!. Vui lòng kiểm tra lại !"; die;}       
    $config['App']['themes']['default'] = array(
        'front' =>$front,
        'back' => 'Green'
    ); 

    //end 
    //load config Mail
    $config_mail = null;
    $site_current = Configure::read('App.themes.site_current');
    $sql = "SELECT *";
    $sql .= " FROM config_mail";
    $sql .= " WHERE config_mail.sub_site_id = ".$site_current['sub_sites']['id'];
    $sql .= " AND config_mail.set_default = 1";
    $config_mail = $db->query($sql);
    if(!empty($config_mail)){
       Configure::write('App.themes.config_mail',$config_mail); 
    }    
    //End
    $config['Authorize']['activated'] = true;
    $config['Authorize']['antidetect']['all']['activated'] = true;
    $config['Authorize']['antidetect'][0] = 'Basic';
    $config['Authorize']['authenticate']['all']['userModel'] = 'User.User';
    $config['Authorize']['authenticate']['all']['patterns']['email'] = '/\\w{1,}[@][\\w\\-]{1,}([.]([\\w\\-]{1,})){1,3}$/';
    $config['Authorize']['authenticate']['all']['scope']['enabled'] = true;
    $config['Authorize']['authenticate']['all']['contain'] = array('Role');
    $config['Cache']['check'] = true;
    $config['Cache']['clearnup'] = true;
    $config['Cache']['disable'] = false;
    $config['Cache']['duration'] = 2592000;
    $config['Cache']['maxsize'] = 200;
    $config['Datetime']['firstDayOfWeek'] = 1;
    $config['Datetime']['timeZone'] = 'Asia/Bangkok';
    $config['Datetime']['date']['short'] = 'Y-m-d';
    $config['Datetime']['date']['medium'] = 'D, m/d/Y';
    $config['Datetime']['date']['long'] = 'l, F j, Y';
    $config['Datetime']['time']['short'] = 'H:i';
    $config['Datetime']['time']['medium'] = 'H:i:s';
    $config['Datetime']['time']['long'] = 'H:i:s';
    $config['Information']['email'] = 'vantienvnn@gmail.com';
    $config['Information']['name'] = 'TVPANEL POWER';
    $config['Information']['sologan'] = '';
    $config['Information']['title'] = '';
    $config['Information']['keyword'] = '';
    $config['Information']['description'] = '';
    //Config Language
    $sql = "SELECT *";
    $sql .= " FROM config_language";
    $config_language = $db->query($sql);            
    if(empty($config_language)){
        echo "Lỗi: Bạn phải cấu hình ít nhất một ngôn ngữ cho hệ thống. Vui lòng xem lại !";die;
    }
    Configure::write('App.Languages.config_language',$config_language); 
    foreach($config_language as $k=>$v){
        if($v['config_language']['active']==1){
            $config['Routing']['language'] = $v['config_language']['language_code']; 
            Configure::write('App.Languages.languageCurrent',$v);            
        }                
        $config['Languages'][$v['config_language']['language_code']]['charset'] = 'utf-8';
        $config['Languages'][$v['config_language']['language_code']]['code'] = $v['config_language']['language_code'];
        $config['Languages'][$v['config_language']['language_code']]['locale'] = $v['config_language']['prefix'];
        $config['Languages'][$v['config_language']['language_code']]['direction'] = 'ltr';
        $config['Languages'][$v['config_language']['language_code']]['domain'] = $v['config_language']['language_code'].'.greencms.toancauxanh.vn';
        $config['Languages'][$v['config_language']['language_code']]['encodingLists'] = '';        
        $config['Languages'][$v['config_language']['language_code']]['name'] = $v['config_language']['name'];
        $config['Languages'][$v['config_language']['language_code']]['native'] = $v['config_language']['name'];
        $config['Languages'][$v['config_language']['language_code']]['prefix'] = $v['config_language']['prefix'];
        $config['Languages'][$v['config_language']['language_code']]['datetime'] = array();
        $config['Languages'][$v['config_language']['language_code']]['path'] = $v['config_language']['path'];        
    }        
    //End    
    $config['Logging']['enabled'] = true;
    $config['Logging']['limit'] = 10000;
    $config['Logging']['types']['warning'] = true;
    $config['Logging']['types']['notice'] = true;
    $config['Logging']['types']['error'] = true;
    $config['Logging']['types']['debug'] = true;
    $config['Logging']['types']['info'] = true;
    $config['Logging']['types']['event'] = true;
    $config['Logging']['types']['login'] = true;
    $config['Logging']['types']['created'] = true;
    $config['Logging']['types']['updated'] = true;
    $config['Logging']['types']['deleted'] = true;
    $config['Maintenance']['activated'] = false;
    $config['Maintenance']['message'] = 'Localhost is currently under maintenance. We should be back shortly. Thank you for your patience.';
    $config['Maintenance']['time'] = '2012-3-17 11:22';
    $config['Maintenance']['allowedActions'][0] = 'login';
    $config['Maintenance']['allowedActions'][1] = 'captcha';
    $config['Maintenance']['cacheCheck'] = false;
    $config['Modules']['System']['autoConfig'] = false;
    $config['Modules']['System']['description'] = '';
    $config['Modules']['System']['name'] = 'System';
    $config['Modules']['System']['routes'] = true;
    $config['Modules']['User']['autoConfig'] = false;
    $config['Modules']['User']['description'] = '';
    $config['Modules']['User']['name'] = 'User';
    $config['Modules']['User']['helpers'] = array('User.UserHook');
    $config['Modules']['User']['routes'] = true;
    $config['Modules']['Node']['autoConfig'] = false;
    $config['Modules']['Node']['description'] = '';
    $config['Modules']['Node']['name'] = 'Node';
    $config['Modules']['Node']['routes'] = true;
    //$config['Modules']['Node']['helpers'] = array('Node.NodeHook');
    $config['Modules']['Regional']['autoConfig'] = false;
    $config['Modules']['Regional']['description'] = '';
    $config['Modules']['Regional']['name'] = 'Regional';
    $config['Modules']['Regional']['routes'] = true;
    $config['Modules']['Block']['autoConfig'] = false;
    $config['Modules']['Block']['description'] = '';
    $config['Modules']['Block']['name'] = 'Block';
    $config['Modules']['Block']['routes'] = true;
    $config['Modules']['Menu']['autoConfig'] = false;
    $config['Modules']['Menu']['description'] = '';
    $config['Modules']['Menu']['name'] = 'Menu';
    $config['Modules']['Menu']['routes'] = true;
    //View module 
    $config['Modules']['View']['autoConfig'] = false;
    $config['Modules']['View']['description'] = '';
    $config['Modules']['View']['name'] = 'View';
    $config['Modules']['View']['routes'] = true;
    $config['Modules']['View']['helpers'] = array('View.ViewHook');
    //$config['Modules']['System']['helpers'][0] = 'System.Validate';
    $config['Routing']['detection']['browser'] = false;
    $config['Routing']['detection']['session'] = false;
    $config['Routing']['detection']['parameter'] = 'language';
    $config['Routing']['detection']['enabled'] = true;
    $config['Routing']['detection']['redirect'] = true;
    $config['Routing']['detection']['isPrefix'] = true;
    $config['Routing']['extension'][0] = 'html';
    $config['Routing']['extension'][1] = 'xml';
    $config['Routing']['extension'][2] = 'rss';    
    $config['Routing']['prefixes'] = array('admin', 'manage');
    //$config['Routing']['route'] = '/event/khuyen_mai_thang_5.html';
    $config['Session']['defaults'] = 'php';
    $config['Session']['checkAgent'] = false; //test, set true if not test
    $config['Session']['cookie'] = 'TVPANEL';
    $config['Session']['timeout'] = 240;
    //$config['Session']['cookieTimeout'] = 0;
    $config['debug'] = 2;
?>